import pandas as pd
import matplotlib.pyplot as plt


def energy_con_pre():
    # 读取 2019 年数据并处理
    energy_con_2019 = pd.read_csv("2019/energy_con.csv")
    print(energy_con_2019.describe(include='all'))
    energy_con_2019['year'] = 2019  # 添加年份列
    energy_con_2019['LA code'] = energy_con_2019['LA code'].str[-4:].astype(int)  # 处理 LA code

    # 读取 2020 年数据并处理
    energy_con_2020 = pd.read_csv("2020/energy_con.csv")
    print(energy_con_2020.describe(include='all'))
    energy_con_2020['year'] = 2020  # 添加年份列
    energy_con_2020['LA code'] = energy_con_2020['LA code'].str[-4:].astype(int)  # 处理 LA code

    # 读取 2021 年数据并处理
    energy_con_2021 = pd.read_csv("2021/energy_con.csv")
    print(energy_con_2021.describe(include='all'))
    energy_con_2021['year'] = 2021  # 添加年份列
    energy_con_2021['LA code'] = energy_con_2021['LA code'].str[-4:].astype(int)  # 处理 LA code

    # 读取 2022 年数据并处理
    energy_con_2022 = pd.read_csv("2022/energy_con.csv")
    print(energy_con_2022.describe(include='all'))
    energy_con_2022['year'] = 2022  # 添加年份列
    energy_con_2022['LA code'] = energy_con_2022['LA code'].str[-4:].astype(int)  # 处理 LA code

    # 合并所有年份的数据
    combined_energy_con = pd.concat([energy_con_2019, energy_con_2020, energy_con_2021, energy_con_2022], ignore_index=True)
    # 统计缺失值情况
    missing_stats = combined_energy_con.isnull().sum()
    # 打印缺失值统计
    print(missing_stats)
    #根据total创建分类特征
    # 计算分布区间
    low_threshold = combined_energy_con['Construction (Total tCO2e)'].quantile(0.33)  # 小的上限（分位点 33%）
    mid_threshold = combined_energy_con['Construction (Total tCO2e)'].quantile(0.66)  # 中的上限（分位点 66%）
    # 直接创建分类变量列
    combined_energy_con['Construction_Category'] = 'Large'  # 默认赋值为 "大"
    combined_energy_con.loc[combined_energy_con['Construction (Total tCO2e)'] <= mid_threshold, 'Construction_Category'] = 'Medium'
    combined_energy_con.loc[combined_energy_con['Construction (Total tCO2e)'] <= low_threshold, 'Construction_Category'] = 'Small'
    combined_energy_con.to_csv('energy_con_2019_2022.csv',index=None)




# 2. Plotting distributions using histograms
# 3. ldentifying outliers using boxplots
# 4. Plotting timeseries
def energy_con_show(col_name):
    combined_energy_con = pd.read_csv('energy_con_2019_2022.csv')
    plt.figure(figsize=(8, 6))
    plt.hist(combined_energy_con[col_name], bins=10, edgecolor='black', alpha=0.7)

    plt.title(f'Distribution of {col_name}', fontsize=16)
    plt.xlabel(f'{col_name}', fontsize=14)
    plt.ylabel('Frequency', fontsize=14)

    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.show()

    # Plot a boxplot
    plt.figure(figsize=(8, 6))
    plt.boxplot(
        combined_energy_con[col_name].dropna(),
        vert=False,
        patch_artist=True,
        boxprops=dict(facecolor='lightblue')
    )

    # Add title and labels
    plt.title(f'Boxplot of {col_name}', fontsize=16)
    plt.xlabel('Total tCO2e', fontsize=14)

    # Display the plot
    plt.grid(axis='x', linestyle='--', alpha=0.7)
    plt.show()

    # Identify outliers
    q1 = combined_energy_con[col_name].quantile(0.25)  # 25th percentile
    q3 = combined_energy_con[col_name].quantile(0.75)  # 75th percentile
    iqr = q3 - q1  # Interquartile Range (IQR)

    # Define bounds for outliers
    lower_bound = q1 - 1.5 * iqr
    upper_bound = q3 + 1.5 * iqr

    # Filter out outliers
    outliers = combined_energy_con[
        (combined_energy_con[col_name] < lower_bound) |
        (combined_energy_con[col_name] > upper_bound)
        ]

    print("Outlier Range:")
    print(f"Lower Bound: {lower_bound}, Upper Bound: {upper_bound}")
    print("\nOutlier Data:")
    print(outliers)

    selected_la_codes = combined_energy_con['LA code'].unique()[:5]

    filtered_data = combined_energy_con[combined_energy_con['LA code'].isin(selected_la_codes)]

    # Plot the time series
    plt.figure(figsize=(10, 6))
    for la_code in selected_la_codes:
        la_data = filtered_data[filtered_data['LA code'] == la_code]
        plt.plot(la_data['year'], la_data[col_name], marker='o', label=f"LA code {la_code}")

    # Add title, labels, and legend
    plt.title(f'Time Series of {col_name} for Selected LA Codes', fontsize=16)
    plt.xlabel('Year', fontsize=14)
    plt.ylabel(f'{col_name}', fontsize=14)
    plt.legend(title='LA Code', fontsize=12)
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.show()

if __name__ == '__main__':
    energy_con_show('Diesel (CO2e)')